package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author mqx
 * @date 2020-10-24 09:48:58
 */
@Component  // 注入到spring 容器中
public class DeadLetterReceiver {

    // 使用注解
    //    @RabbitListener(bindings = @QueueBinding(
    //            value = @Queue(value = ""),
    //            exchange = @Exchange(value = ""),
    //            key = {""}
    //    ))
    //    public void getMsg(){
    //
    //    }

    @SneakyThrows
    @RabbitListener(queues = DeadLetterMqConfig.queue_dead_2)
    public void getMsg(String msg, Message message, Channel channel){

        System.out.println("接收到的消息："+msg);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Receive queue_dead_2: " + sdf.format(new Date()) + " Delay rece." + msg);

        //  false 只删除一次
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
